package com.jk.controller;

import com.jk.po.HotelPO;
import com.jk.po.PersonalPO;
import com.jk.service.HotelService;
import com.jk.vo.ResultInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 段卓帆
 * @date 2021/6/30 11:23
 * @return 酒店
 */
@RestController
@RequestMapping("hotel")
@Api("酒店操作接口")
public class HotelController {

    //日志记录对象
    private static Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private HotelService hotelService;

    /**
     * @author 段卓帆
     * @date 2021/6/30 11:26
     * @return 查询酒店信息
     */
    @GetMapping("queryHotel")
    @ApiOperation("查询酒店信息")
    public void queryHotel() throws IOException {
        //获取路径
        File file = new File("");
        String path =file.getCanonicalPath();
        //字符串拼接路径
        String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"hotel"+"/";


        List<HotelPO> hotelPOS = hotelService.queryHotel();

        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
        Configuration configuration = null;
        FileWriter fileWriter = null;
        try{
            try {
                configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                Template template = configuration.getTemplate("hotel.ftl");
                fileWriter = new FileWriter(filePath + "hotel" + ".html");
                Map<String,List<HotelPO>> map = new HashMap<String, List<HotelPO>>();
                map.put("hotel",hotelPOS);
                template.process(map,fileWriter);
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if ( fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * @author 段卓帆
     * @date 2021/6/30 14:50
     * @return 查看酒店详情
     */
    @PostMapping ("queryHotelById/{id}")
    @ApiOperation(value = "回显酒店信息",produces ="id")
    public HotelPO queryHotelById(@PathVariable("id")@ApiParam("酒店id")Integer id){
        HotelPO hotelPO = new HotelPO();
        try {
            hotelPO =  hotelService.queryHotelById(id);
            logger.info("回显酒店信息成功");
        } catch (Exception e){
            logger.error("回显酒店信息失败:"+e.getMessage());
        }
        return hotelPO;
    }



    /**
     * @author 段卓帆
     * @date 2021/6/30 14:50
     * @return 预定酒店()
     */



}
